// import "babel-polyfill";
// import Promise from "es6-promise";
// Promise.polyfill();
import HeaderRight from './components/common/headerRight.vue'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Vuex from 'vuex'
import VueI18n from 'vue-i18n'
import './processConfig.js'

import App from './App.vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './tools/index.js'
import filters from './filters.js'
import store from './store/index.js'
import ripples, { configSystemRoute } from 'ripples'
import Api_SSDP_Mapping from './SSDPConfig'
import routeConfig from './router'
import iView from 'view-design'
import * as echarts from 'echarts'
import { CoreModuleName } from './const.js'

Vue.prototype.$echarts = echarts

// import 'ripples/dist/styles/ripples.css'
//  import '@ripples/iview-crcloud-theme' // 华润云风格主题
// import '@ripples/iview-crc-theme' // 集团风格主题
import '@/styles/index.less'

// import ripplesEnSource from 'ripples/dist/locale/en-US'
// import ripplesZhSource from 'ripples/dist/locale/zh-CN'
// import langSource from './lang'
import coverApi from '@/api/cover.js'
import format from './components/common/Format'
import UmyUi from 'umy-ui'
Vue.component('Format', format)
Vue.component('HeaderRightSlot', HeaderRight)
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(VueI18n)
Vue.use(ElementUI)
Vue.use(UmyUi)

Vue.config.devtools = window.RPConfig.devtools
// Vue Dev tools
if (process.env.NODE_ENV === 'development') {
  Vue.config.devtools = true
}
window.RPConfig.hiddenTagCon = true
window.RPConfig.showHeaderRoute = true

for (let filter of filters) {
  Vue.filter(filter.name, filter.fn)
}

// 自动设置语言
// const navLang = navigator.language
// const localLang = navLang === 'zh-CN' || navLang === 'en-US' ? navLang : false
// const lang = window.localStorage.getItem('language') || localLang || 'zh-CN'
// Vue.config.lang = lang

var routes = []
configSystemRoute(routes, routeConfig)
const router = new VueRouter({ routes })

const { openSSDP, ssdp_config } = window.RPConfig
const SSDPConfig = !!openSSDP && ssdp_config && Reflect.ownKeys(ssdp_config).length ? ssdp_config : Api_SSDP_Mapping

Vue.use(ripples, {
  router,
  store,
  Api_SSDP_Mapping: SSDPConfig,
  app: {
    title: '通用开发平台',
    //配置主页路由信息
    home: {
      path: '/home',
      title: '首页',
      name: 'home_index'
    }
  }
})

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start()
  // let routeList = routeConfig.children
  // let currentRoute = routeList.find(item => `/${item.path}` == to.path)
  // if (currentRoute) {
  //   let routeObj = {
  //     actionUrl: currentRoute.path,
  //     title: currentRoute.title,
  //     path: currentRoute.path,
  //     query: to.query,
  //     params: to.params,
  //     name: currentRoute.path,
  //   }
  //   window.$store.commit('ripples-components/chengeHeaderRouteList', [routeObj])
  // }
  $channel.$emit('RouterChangeBefore')
  $channel.$emit('ActionBarEvent', [])
  next()
  iView.LoadingBar.finish()
})

router.afterEach(() => {
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})

// 多语言配置
// Vue.locale('zh-CN', Object.assign(ripplesZhSource, langSource['zh-CN']))
// Vue.locale('en-US', Object.assign(ripplesEnSource, langSource['en-US']))

window.$channel = window.$channel || new Vue()
import coverTable from './coverTable/App.vue'
let vueOption = {
  el: '#app',
  router, // 路由配置
  store,
  render: h => h(coverTable),
  data: {
    currentPageName: ''
  },
  mounted() {
    this.currentPageName = this.$route.name
  },
  created() {}
}

window.$app = new Vue(vueOption)
window.$store = store
Vue.prototype.$store = store
// store.commit(CoreModuleName + '/setPublicPages', 'history-update-list')
// 预加载
const onPreloading = src => {
  const iframe = document.createElement('iframe')
  iframe.style.width = 0
  iframe.style.height = 0
  iframe.style.display = 'none'
  iframe.setAttribute('src', src)
  document.body.appendChild(iframe)
}
// 获取加载地址
const getIframeUrlList = () => {
  if (!localStorage.Authorization) {
    return false
  }
  coverApi.getCodeList('IFRAME_OUT_URL').then(res => {
    const iframeUrlList = []
    let rescords = res.data.records
    if (rescords.length > 0) {
      rescords.forEach(item => {
        let url = item.vsiDescription.split('/#/')[0] || ''
        if (url.includes('#/')) {
          // 低代码平台的url不正规，有这类地址https://signon-dadp.crcloud.com/static/portal/vue/index.html#/view_blank?id=__5E48bjVndAZA7zZY
          url = item.vsiDescription.split('#/')[0] || ''
        }
        if (url && !iframeUrlList.includes(url)) {
          iframeUrlList.push(url)
        }
        // 因为数字化中台home页不触发登录操作，所以对数字化中台引入的页面做特殊处理
        // 以数字化中台用户管理为特殊标识对数字化中台进行处理：domain-manage/user-list
        // const fullUrl = item.vsiDescription || '';
        // if (fullUrl.includes('domain-manage/user-list')) {
        //   iframeUrlList.push(fullUrl);
        // }
      })
      iframeUrlList.forEach(item => {
        onPreloading(item)
      })
    }
  })
}
// 如果被嵌入到iframe内，不需要加载本系统嵌入的其它系统页面
if (window.top === window.self) {
  console.log('IT共享未被嵌入到iframe，开始加载其它系统页面')
  getIframeUrlList()
} else {
  console.log('IT共享被嵌入到iframe，放弃加载其它系统页面')
}

// 监听iframe嵌入的子应用发生的消息，用于实现token失效同步问题
window.addEventListener(
  'message',
  function(event) {
    if (event.data === 'ripples:sso:loginout') {
      window.$util.goToLogin()
    }
  },
  false
)
